
#include "..\UnitTest\ut_features.h"
#include "..\UnitTest\ut_runner.h"

//#include "cx_platform_base.h"
#include "cx_stack.h"
#include "cx_memory.h"

/*************************************************************************
* UT CASE
*************************************************************************/

UT_CASE( __test_enter )
{
	return UT_RESULT_PASS;
}

UT_CASE( __cx_stack_new_test )
{
	QStack* stack = cx_stack_new(QTRUE);
	UT_EXPECT_OR_RETURN( stack != NULL, UT_RESULT_FAIL );

	if (stack != NULL)
	{
		cx_stack_releaseRef(stack);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_stack_newEx_test )
{
	QStack* stack = cx_stack_new(QTRUE);
	UT_EXPECT_OR_RETURN( stack != NULL, UT_RESULT_FAIL );

	if (stack != NULL)
	{
		cx_stack_releaseRef(stack);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_stack_count_test )
{
	int* obj = QNULL;
	QStack* stack = QNULL;
	stack = cx_stack_new(QTRUE);
	UT_EXPECT_OR_RETURN( stack != NULL, UT_RESULT_FAIL );

	obj = (QINT*)cx_malloc(sizeof(QINT));
	cx_stack_push(stack, obj);
	UT_EXPECT_OR_RETURN( cx_stack_count(stack) == 1, UT_RESULT_FAIL );

	if (stack != NULL)
	{
		cx_stack_releaseRef(stack);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_stack_push_test )
{
	int* obj = QNULL;
	QStack* stack = QNULL;
	QBOOL ret = QFALSE;
	
	stack = cx_stack_new(QTRUE);
	UT_EXPECT_OR_RETURN( stack != NULL, UT_RESULT_FAIL );

	obj = (int*)cx_malloc(sizeof(int));
	ret = cx_stack_push(stack, obj);
	UT_EXPECT_OR_RETURN( ret == QTRUE, UT_RESULT_FAIL );
	UT_EXPECT_OR_RETURN( cx_stack_count(stack) == 1, UT_RESULT_FAIL );

	if (stack != NULL)
	{
		cx_stack_releaseRef(stack);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_stack_pop_test )
{
	int* obj = QNULL;
	QINT* obj_o = QNULL;
	QStack* stack = QNULL;
	
	stack = cx_stack_new(QTRUE);
	UT_EXPECT_OR_RETURN( stack != NULL, UT_RESULT_FAIL );

	obj = (int*)cx_malloc(sizeof(int));
	*obj = 5;
	cx_stack_push(stack, obj);
	UT_EXPECT_OR_RETURN( cx_stack_count(stack) == 1, UT_RESULT_FAIL );

	obj = (int*)cx_malloc(sizeof(int));
	*obj = 6;
	cx_stack_push(stack, obj);
	UT_EXPECT_OR_RETURN( cx_stack_count(stack) == 2, UT_RESULT_FAIL );

	obj_o = (QINT*) cx_stack_pop(stack);
	UT_EXPECT_OR_RETURN( cx_stack_count(stack) == 1, UT_RESULT_FAIL );
	UT_EXPECT_OR_RETURN( *obj_o == 6, UT_RESULT_FAIL );

	cx_free(obj_o);

	if (stack != NULL)
	{
		cx_stack_releaseRef(stack);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __test_leave )
{
	return UT_RESULT_PASS;
}

/*************************************************************************
* UT GROUP
*************************************************************************/

UT_GROUP( AcceptanceTest_CX_STACK )
{
	UT_ADD_CASE( __test_enter ),
	UT_ADD_CASE( __cx_stack_new_test ),
	UT_ADD_CASE( __cx_stack_newEx_test ),
	UT_ADD_CASE( __cx_stack_count_test ),
	UT_ADD_CASE( __cx_stack_push_test ),
	UT_ADD_CASE( __cx_stack_pop_test ),
	UT_ADD_CASE( __test_leave ),

	UT_LAST_CASE
};
